package com.itboye.hutouben.activity.driverrepair.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.driverrepair.ActivityZhifu;
import com.itboye.hutouben.activity.mysetting.SettingActivity;
import com.itboye.hutouben.base.BaseFragment;
import com.itboye.hutouben.base.ptr.BasePtr;
import com.itboye.hutouben.bean.GoPayBean;
import com.itboye.hutouben.bean.RepairBean;
import com.itboye.hutouben.fragment.DaoLuFragment;
import com.itboye.hutouben.presenter.SysTemMessgelPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

import static com.itboye.hutouben.util.SPUtils.getSp;


/**
 * 订单界面
 */
public class OrderTvFragment extends BaseFragment implements Observer {
    TextView master_name;
    TextView maseter_num, master_good_at_1, master_good_at_2, phone_tv, chakandingdan;
    RelativeLayout check_order_rel;//去支付
    RelativeLayout finish_rel;//联系师傅
    RelativeLayout finish_relwanchengss;// 等待师傅维修完成
    RelativeLayout finish_relwancheng;//维修完成

    SysTemMessgelPresenter presenter;
    RepairBean bean;
    RelativeLayout order_rel, Repair_finish_rel;
    PtrFrameLayout commend_anchor_ptr;

    String uid;
    @Override
    public int initView() {
        return R.layout.fragment_order_tv;

    }

    @Override
    public void onMyActivityCreated() {
        uid = getSp(getActivity(), Const.ID);
        bean = new RepairBean();
        presenter = new SysTemMessgelPresenter(this);
        presenter.onByRepairCurrent(uid, "6");

        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新

        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                uid = getSp(getActivity(), Const.ID);
                presenter.onByRepairCurrent(uid, "6");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                uid = getSp(getActivity(), Const.ID);
                presenter.onByRepairCurrent(uid, "6");
            }
        });
        commend_anchor_ptr.autoRefresh();
//        IntentFilter intentFilterExit = new IntentFilter(
//                "GoPayActivity");
//        getActivity().registerReceiver(nichengBroadcastReceiver, intentFilterExit);

        // 维修结束
        IntentFilter filter1 = new IntentFilter("weixiu");
        getActivity().registerReceiver(emailBroadcastReceiver, filter1);

        //登录的广播
        IntentFilter filter = new IntentFilter(SettingActivity.EXITCHANGE);
        getActivity().registerReceiver(loginReceiver, filter);

    }

    BroadcastReceiver loginReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            uid = getSp(getActivity(), Const.ID);
            presenter.onByRepairCurrent(uid, "6");
        };
    };
    BroadcastReceiver emailBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            uid = getSp(getActivity(), Const.ID);
            presenter.onByRepairCurrent(uid, "6");
            DaoLuFragment.getInstance().viewPager.setCurrentItem(1);
        };
    };

    @Override
    public void onResume() {
        super.onResume();
        presenter.onByRepairCurrent(uid, "6");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(emailBroadcastReceiver);
        getActivity().unregisterReceiver(loginReceiver);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.check_order_rel:
                break;
            case R.id.finish_rel:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + bean.getWorker_info().getMobile());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.phone_tv:
                Intent intent01 = new Intent(Intent.ACTION_DIAL);
                Uri data01 = Uri.parse("tel:" + bean.getWorker_info().getMobile());
                intent01.setData(data01);
                startActivity(intent01);
                break;

        }

    }
    @Override
    public void update(Observable observable, Object data) {

        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.Repair_current_success) {
                try {
                    closeProgressDialog();
                } catch (Exception e) {

                }
                bean = (RepairBean) entity.getData();
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                if (bean.getMobile() == null || bean.getMobile() == "") {
                    order_rel.setVisibility(View.GONE);
                    Repair_finish_rel.setVisibility(View.VISIBLE);

                }
                if (!bean.getRepair_status().toString().equals("")) {

                    Log.d("getrewrwe", bean.getMobile() + "");
                    if (bean.getRepair_status().equals("") || bean.getRepair_status() == null) {
                        order_rel.setVisibility(View.GONE);
                        Repair_finish_rel.setVisibility(View.VISIBLE);
                    } else if (bean.getRepair_status().equals("0")) {
                        order_rel.setVisibility(View.GONE);
                        Repair_finish_rel.setVisibility(View.VISIBLE);
                    } else if (bean.getRepair_status().equals("1")) {
                        order_rel.setVisibility(View.VISIBLE);
                        Repair_finish_rel.setVisibility(View.GONE);

                        finish_rel.setVisibility(View.VISIBLE);
                        check_order_rel.setVisibility(View.GONE);
                        finish_relwanchengss.setVisibility(View.GONE);
                        finish_relwancheng.setVisibility(View.GONE);
                        maseter_num.setText(bean.getWorker_info().getUid());
                        phone_tv.setText(bean.getWorker_info().getMobile());

                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < bean.getWorker_info().getWorker_skills().size(); i++) {
                            sb.append(bean.getWorker_info().getWorker_skills().get(i).getName() + ",");
                        }
                        String name = sb.substring(0, sb.length() - 1);
                        master_good_at_1.setText(name);

                    } else if (bean.getRepair_status().equals("2")) {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < bean.getWorker_info().getWorker_skills().size(); i++) {
                            sb.append(bean.getWorker_info().getWorker_skills().get(i).getName() + ",");
                        }
                        String name = sb.substring(0, sb.length() - 1);
                        master_good_at_1.setText(name);
                        maseter_num.setText(bean.getWorker_uid());
                        phone_tv.setText(bean.getWorker_info().getMobile());
                        order_rel.setVisibility(View.VISIBLE);
                        Repair_finish_rel.setVisibility(View.GONE);

                        finish_rel.setVisibility(View.GONE);
                        check_order_rel.setVisibility(View.VISIBLE);
                        finish_relwanchengss.setVisibility(View.GONE);
                        finish_relwancheng.setVisibility(View.GONE);
                        chakandingdan.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.onZhiFu(IsUtilUid.isUid(),bean.getId(),"","","","","");
                            }
                        });
                        check_order_rel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.onZhiFu(IsUtilUid.isUid(),bean.getId(),"","","","","");
                            }
                        });
                    } else if (bean.getRepair_status().equals("3")) {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < bean.getWorker_info().getWorker_skills().size(); i++) {
                            sb.append(bean.getWorker_info().getWorker_skills().get(i).getName() + ",");
                        }
                        String name = sb.substring(0, sb.length() - 1);

                        order_rel.setVisibility(View.VISIBLE);
                        Repair_finish_rel.setVisibility(View.GONE);
                        finish_rel.setVisibility(View.GONE);
                        check_order_rel.setVisibility(View.VISIBLE);
                        finish_relwanchengss.setVisibility(View.GONE);
                        finish_relwancheng.setVisibility(View.GONE);
                        master_good_at_1.setText(name);
                        maseter_num.setText(bean.getWorker_uid());
                        phone_tv.setText(bean.getWorker_info().getMobile());
                        chakandingdan.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.onZhiFu(IsUtilUid.isUid(),bean.getId(),"","","","","");
                            }
                        });
                        check_order_rel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.onZhiFu(IsUtilUid.isUid(),bean.getId(),"","","","","");
                            }
                        });

                    } else if (bean.getRepair_status().equals("4")) {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < bean.getWorker_info().getWorker_skills().size(); i++) {
                            sb.append(bean.getWorker_info().getWorker_skills().get(i).getName() + ",");
                        }
                        String name = sb.substring(0, sb.length() - 1);
                        order_rel.setVisibility(View.VISIBLE);
                        Repair_finish_rel.setVisibility(View.GONE);
                        finish_rel.setVisibility(View.GONE);
                        check_order_rel.setVisibility(View.GONE);
                        finish_relwanchengss.setVisibility(View.VISIBLE);
                        finish_relwancheng.setVisibility(View.GONE);
                        master_good_at_1.setText(name);
                        maseter_num.setText(bean.getWorker_uid());
                        phone_tv.setText(bean.getWorker_info().getMobile());


                    } else if (bean.getRepair_status().equals("5")) {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < bean.getWorker_info().getWorker_skills().size(); i++) {
                            sb.append(bean.getWorker_info().getWorker_skills().get(i).getName() + ",");
                        }
                        String name = sb.substring(0, sb.length() - 1);
                        master_good_at_1.setText(name);
                        maseter_num.setText(bean.getWorker_uid());
                        phone_tv.setText(bean.getWorker_info().getMobile());
                        order_rel.setVisibility(View.VISIBLE);
                        Repair_finish_rel.setVisibility(View.GONE);
                        finish_rel.setVisibility(View.GONE);
                        check_order_rel.setVisibility(View.GONE);
                        finish_relwanchengss.setVisibility(View.GONE);
                        finish_relwancheng.setVisibility(View.VISIBLE);
                        finish_relwancheng.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                showProgressDialog("请稍后", true);
                                presenter.onWieXiuWanchng(IsUtilUid.isUid(), bean.getId(), "6");
                            }
                        });
                    }

                } else {
                    order_rel.setVisibility(View.GONE);
                    Repair_finish_rel.setVisibility(View.VISIBLE);
                }

            }
            //去支付
            if (entity.getEventType() == SysTemMessgelPresenter.zhifu_success) {
                GoPayBean  gobean = (GoPayBean) entity.getData();
                Intent intent=new Intent(getActivity(), ActivityZhifu.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("bean", gobean);
                intent.putExtras(bundle);
                intent.putExtra("id",bean.getId());
                startActivity(intent);
            }
            if (entity.getEventType() == SysTemMessgelPresenter.zhifu_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == presenter.WanCheng_success) {
                ByAlert.alert(entity.getData() + "");
              //  getActivity().finish();
                try {
                    closeProgressDialog();
                } catch (Exception e) {

                }
            }
            if (entity.getEventType() == presenter.WanCheng_fail) {
                ByAlert.alert(entity.getData() + "");
                try {
                    closeProgressDialog();
                } catch (Exception e) {

                }
            }
        }

    }
}
